package game.map
{
	import flash.geom.Point;
	import game.manager.ConfigurationManager;
	import game.manager.MapManager;
	import game.manager.TextureManager;
	import starling.display.BlendMode;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.textures.Texture;
	
	/**
	 * ...
	 * @author Kirill Pletnev
	 */
	public class BackgraundContainer extends Sprite
	{
		private var _visualMap:VisualMap;
		private var _isInit:Boolean = false;
		
		public function BackgraundContainer(visualMap:VisualMap)
		{
			this._visualMap = visualMap;
			
			this.dispose();
			this.blendMode = BlendMode.NONE;
			this.x = -visualMap.mapHalfWidth;
			this.y = 0;
		}
		
		public function initBackgraund():void
		{
			TextureManager.instance.load(ConfigurationManager.instance.resPath + MapManager.instance.backgroundTextureURL, onTextureLoad);
		}
		
		public function onTextureLoad(bgTexture:Texture):void
		{
			bgTexture.repeat = true;
			
			var bgImage:Image = new Image(bgTexture);
			bgImage.width = _visualMap.mapWidth;
			bgImage.height = _visualMap.mapHeigth;
			
			var repeWidth:uint = _visualMap.mapWidth / bgTexture.width + 1;
			var repeHeigth:uint = _visualMap.mapHeigth / bgTexture.height + 1;
			
			bgImage.setTexCoords(1, new Point(repeWidth, 0));
			bgImage.setTexCoords(2, new Point(0, repeHeigth));
			bgImage.setTexCoords(3, new Point(repeWidth, repeHeigth));
			
			this.addChild(bgImage);
			this.flatten();
			
			_isInit = true;
			SplashScreen.instance.nextStep();
			
			trace("BackgraundContainer initialized");
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		public function get isInit():Boolean
		{
			return _isInit;
		}
	}

}